﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PHTShop.admin
{
    public partial class editCategory : System.Web.UI.Page
    {
        DataContextDataContext dc = new DataContextDataContext();
       

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Edit category";

            if (!IsPostBack)
            {
                int catId = Convert.ToInt32(Request.QueryString["id"]);

                var list = dc.CategoriesLanguages.Where(z => z.categoriesId == catId);
               
                foreach (var item in list)
                {

                    switch (item.languageId)
                    {
                        case 1:
                            CatName_en.Value = item.name;
                            break;
                        case 2:
                            CatName_fr.Value = item.name;
                            break;
                        case 3:
                            CatName_gr.Value = item.name;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int catId = Convert.ToInt32(Request.QueryString["id"]);

            var list = dc.CategoriesLanguages.Where(z => z.categoriesId == catId);
            string catEn = CatName_en.Value.Trim();
            string catFr = CatName_fr.Value.Trim();
            string catGr = CatName_gr.Value.Trim();
            if (string.IsNullOrEmpty(catEn) && string.IsNullOrEmpty(catFr) && string.IsNullOrEmpty(catGr))
            {
                labelError.Text = "Category name can not be empty";
            }
            else
            {

                foreach (var item in list)
                {
                    switch (item.languageId)
                    {

                        case 1:
                            if (!string.IsNullOrEmpty(catEn))
                            {
                                update(item.id, catEn);
                            }
                            break;
                        case 2:
                            if (!string.IsNullOrEmpty(catFr))
                            {
                                update(item.id, catFr);
                            }
                            break;
                        case 3:
                            if (!string.IsNullOrEmpty(catGr))
                            {
                                update(item.id, catGr);
                            }
                            break;
                    }
                }

                Response.Redirect("categoryList.aspx");

            }
        }

        private void update(int id, string name)
        {
            CategoriesLanguage c = dc.CategoriesLanguages.SingleOrDefault(z => z.id == id);
            c.name = name;
            dc.SubmitChanges();
        }
    }
}